package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.BuyRecord;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.mapper.BuyRecordMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IBuyRecordService;
import cn.wolfcode.crm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BuyRecordServiceImpl implements IBuyRecordService{
	@Autowired
	private BuyRecordMapper buyRecordMapper;

	@Autowired
	private ICustomerService customerService;

	@Override
	public void insert(BuyRecord buyRecord) {
		buyRecord.setInputTime(new Date());
		Customer customer = buyRecord.getCustomer();
		Long id = customer.getId();
		customerService.updateStatus(id,1);
		buyRecordMapper.insert(buyRecord);
	}


	@Override
	public void update(BuyRecord buyRecord) {
		buyRecordMapper.update(buyRecord);
	}

	@Override
	public BuyRecord select(Long id) {
		BuyRecord buyRecord = buyRecordMapper.select(id);
		return buyRecord;
	}

	@Override
	public List<BuyRecord> list() {
		List<BuyRecord> list = buyRecordMapper.list();
		return list;
	}

	public PageInfo<BuyRecord> query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<BuyRecord> buyRecords = buyRecordMapper.selectForList(qo);
        return new PageInfo<>(buyRecords);
	}



}
